import 'dart:convert';

class Product {
  final String id;
  final String name;
  final List<String> descriptions;
  List<ScriptItem>? scripts;

  Product({
    required this.id,
    required this.name,
    required this.descriptions,
    this.scripts,
  });

  factory Product.fromJson(Map<String, dynamic> json) {
    List<String> descriptions = [];
    if (json['description'] != null) {
      try {
        descriptions = List<String>.from(jsonDecode(json['description']));
      } catch (e) {
        if (json['description'] is String) {
          descriptions = [json['description']];
        }
      }
    }
    
    return Product(
      id: json['id'].toString(),
      name: json['goodsName'] ?? '',
      descriptions: descriptions,
    );
  }
}

class ScriptItem {
  final String text;
  final int duration; // 播放时长（秒）

  ScriptItem({
    required this.text,
    required this.duration,
  });
}

